package testJdk.base.juc_test.multi_threading;

import java.util.HashSet;

/**
 * @author Created by cyd on 2021-03-26 15:32:41
 * @Description : 看hashset的输出值，并分析每种值出现的原因
 */
public class VolatileSeriaTest {
    static int x = 0, y = 0;
    static int a = 0, b = 0;

    public static void main(String[] args) throws InterruptedException {
        HashSet<String> hashSet = new HashSet<>();
        for (int i = 0; i < 10000000; i++) {
            x = 0;
            y = 0;
            a = 0;
            b = 0;
            Thread one = new Thread(() -> {
                a = y;
                x = 1;
            });

            Thread other = new Thread(() -> {
                b = x;
                y = 1;
            });

            one.start();
            other.start();
            // 确保主线程等待这两个线程执行完毕
            one.join();
            other.join();

            hashSet.add("a=" + a + ",b=" + b);
            System.out.println(hashSet);
        }
    }
}
